//
//  OCTabBar.m
//  OCWeiBo
//
//  Created by mac on 16/3/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "OCTabBar.h"
#import "UIView+Extension.h"


@interface OCTabBar()

@property(nonatomic,strong)UIButton *plusButton;

@end

@implementation OCTabBar

-(instancetype)initWithFrame:(CGRect)frame{
    
   self = [super initWithFrame:frame];
    
    if (self) {
        
        //创建一个Button
        UIButton *button = [[UIButton alloc]init];
        
        //Button的相关属性
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"]forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        
        [button addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //设置button的大小
        [button sizeToFit];
        
        //添加到对应的父控件上
        [self addSubview:button];
        
        self.plusButton = button;
        
    }
    
    return self;
    
}

//布局里面的子控件
-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.plusButton.centerX = self.width * 0.5;
    self.plusButton.centerY = self.height * 0.5;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width / 5;
    
    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    //取出子控件的数目
    NSInteger count = self.subviews.count;
    
    NSInteger index = 0;
    
    for (int i = 0; i < count; i ++) {
        
        UIView *view = self.subviews[i];
        
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
        
            //设置宽度和x的值
            view.width = width;
            view.x = width * index;
            
            //在设置完一个view之后，index的下标加上一个值
            index ++;
            
            if ( index == 2) {
                
                index ++;
                
            }
            
        }
        
    }
    
}

-(void)plusButtonClick:(UIButton *)button{
    
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedPlusButtonClick:)]) {
        [self.delegate tabBar:self didSelectedPlusButtonClick:button];
    }
    
}

@end







